package cn.turing.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.turing.model.Student;

/**
 * @version: V1.0
 * @author: walker
 * @className: MyDB
 * @packageName: cn.turing.db
 * @description: 数据库工具类
 * @data: 2019-5-30
 **/

public class MyDB 
{
	/**数据库连接字符串ַ**/
	private static final String url="jdbc:mysql://localhost:3306/studentManager";
	/**用户名**/
	private static final String username="root";
	/**密码**/
	private static final String password="root";
	
	
	/**
	* @author:  walker
	* @methodsName: getConn
	* @description: 获取数据库连接对象
	* @return: Connection
	*/
	public static Connection getConn()
	{
		Connection con=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	* @author:  walker
	* @methodsName: getResultSet
	* @param String sql语句
	* @param Connection con 
	* @description: 
	* @return: ResultSet
	*/
	public static ResultSet getResultSet(Connection con,String sql)
	{
		PreparedStatement ps;
		ResultSet rs=null;
		try
		{
			ps = con.prepareStatement(sql);
			rs=ps.executeQuery();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return rs;
	}
	
	/***
	 * @author:  walker
	 * @methodsName: getResultSet
	 * @description: 
	 * @param con 
	 * @param sql 
	 * @return 
	 */
	public static int executeUpdateSql(Connection con,String sql)
	{
		PreparedStatement ps;
		int result=0;
		try
		{
			ps = con.prepareStatement(sql);
			result=ps.executeUpdate();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	* @author:  walker
	* @methodsName: closeConnection
	* @param Connection 
	* @description:
	*/
	public static void closeConnection(Connection con)
	{
		try
		{
			if(con!=null&&con.isClosed()==false)
			{
				con.close();
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
}
